module PrePractica where 

-- Ejercicio 2 - a
valorAbsoluto :: Float -> Float
valorAbsoluto f = if f < 0 
                    then -f
                    else f

-- Ejercicio 2 - b
bisiesto :: Int -> Bool
bisiesto a = (a `mod` 4==0) && (((a `mod` 100) /= 0) || (a`mod`400==0))

-- Ejercicio 2 - c
factorial :: Int -> Int
factorial 1 = 1
factorial n = n * factorial (n-1)

factorialFold n = foldr (\ x rec -> x*rec ) 1 [x | x<-[1..n]]

-- Ejercicio 2 - d
cantDivisoresPrimos :: Int -> Int
cantDivisoresPrimos n = length [x | x<-[1..n], n `mod` x == 0, primo x]

primo :: Int -> Bool
primo 1 = False
primo 2 = True
primo n = auxPrimo n (n-1)

auxPrimo :: Int -> Int -> Bool
auxPrimo n 1 = True
auxPrimo n m = (n `mod` m /= 0) && auxPrimo n (m-1)


-- Ejercicio 3 - a
inverso :: Float -> Maybe Float
inverso 0 = Nothing
inverso n = Just (1/n)

-- Ejercicio 3 - b
aEntero :: Either Int Bool -> Int
aEntero (Right False) = 0
aEntero (Right True) = 1
aEntero (Left n) = n

-- Ejercicio 4 - a
limpiar :: String -> String -> String
limpiar [] ys = ys
limpiar (x:xs) ys = sacar x (limpiar xs ys)

sacar :: Eq a => a -> [a] -> [a]
sacar _ [] = []
sacar n (x:xs) = if n == x 
                    then sacar n xs 
                    else x:(sacar n xs)

-- Ejercicio 4 - b
difPromedio :: [Float] -> [Float]
difPromedio xs = difPromedioAux p xs
                where p = (foldr (\ x rec -> x + rec) 0 xs)/(intToFloat (length xs))
                
difPromedioAux :: Float -> [Float] -> [Float]
difPromedioAux p [] = []
difPromedioAux p (x:xs) = (x-p):difPromedioAux p xs

intToFloat :: Int -> Float
intToFloat n = fromInteger (toInteger n)


-- Ejercicio 4 - c
todosIguales :: [Int] -> Bool
todosIguales [] = True
todosIguales (x:xs) = foldr (\x rec -> x && rec) True (map ((==)x) (x:xs))

-- Ejercicio 5 
data AB a = Nil | Bin (AB a) a (AB a) deriving Show

nullTree :: AB a -> Bool
nullTree Nil = True
nullTree _ = False

negTree :: AB Bool -> AB Bool
negTree Nil = Nil
negTree (Bin i r d) = Bin (negTree i) (not r) (negTree d)

aaa=Bin (Bin Nil False Nil) True (Bin (Bin Nil True Nil) True Nil)
bbb=Bin (Bin Nil 3 Nil) 10 (Bin (Bin Nil 5 Nil) 2 Nil)

prodTree :: Num a => AB a -> a
prodTree b = foldr (\ x rec -> x*rec) 1 (inOrder b)

inOrder :: AB a -> [a]
inOrder Nil = []
inOrder (Bin i r d) = (inOrder i) ++ [r] ++ (inOrder d)



-- Ejercicio 4 - d

